package com.tsing.wms.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsing.wms.entity.OrderEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tsing.wms.vo.req.SupplierPageReqVO;
import com.tsing.wms.vo.resp.OrderRespVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 订单表 Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2022-04-13
 */
public interface OrderMapper extends BaseMapper<OrderEntity> {

    @Select("select rd.goods_id,rd.amount,g.name goodsName,g.specs,g.price from t_record_detail rd\n" +
            "left join t_goods g on rd.goods_id=g.id\n" +
            "where rd.out_record_id=#{id}")
    List<OrderRespVO.OrderDetailRespVO> selectOrderDetails(@Param("id") String id);

    @Select("<script>" +
            "select o.*,s.name supplierName,u.username userName,u.phone\n" +
            "from t_order o\n" +
            "left join t_supplier s on o.supplier_id=s.id\n" +
            "left join t_user u on o.user_id=u.id\n" +
            "<where>" +
            "<if test=\"id!=null and id!=''\">and s.user_id=#{id}</if>" +
            "<if test=\"reqVO.name!=null and reqVO.name!=''\">and s.name like concat('%',#{reqVO.name},'%')</if>" +
            "</where>" +
            "order by o.id desc" +
            "</script>")
    IPage<OrderRespVO> orderPage(IPage<OrderRespVO> page, @Param("reqVO") SupplierPageReqVO reqVO, @Param("id") String id);

    @Select("select sum(amount) num from t_record_detail where out_record_id=#{id}")
    Long selectGoodsCount(@Param("id") String id);
}
